class PageController < ApplicationController

  # skip_before_filter :verify_authenticity_token, :only => [:logout]

  def login
        # flash[:notice]='欢迎登陆'
  end
  
  def main
       
  end

  def create_login_session
     @user = UserSys.find_by_login(params[:login])
     if verify_rucaptcha?(@user)
       if @user && @user.authenticate(params[:password])
         # cookies[:auth_token] = @user.auth_token
         session[:login] = @user.login
         session[:name] = @user.name
         session[:section_id] = @user.sections_id
         flash[:notice] = "登录成功！"
         redirect_to :welcome

       else
         flash.notice = "用户名密码错误！"
         redirect_to login_path
       end
     else
         flash.notice = "验证码输入错误！"
         redirect_to login_path
     end
   end
  
  
  def ajax_load
       section_id= params[:id]
       ref_alarm=AlarmRecord.find_by_sql("SELECT count(id) num  FROM alarm_record where sections_id=#{section_id} and status='未审核'
                    and TIMESTAMPDIFF(SECOND,created_at,NOW())<200 ").first
       ref_hotel=RecordData.find_by_sql("SELECT count(id) num   FROM record_data where sections_id=#{section_id} and status='未审核'
                    and TIMESTAMPDIFF(SECOND,created_at,NOW())<200 ").first
       ref={alarm:ref_alarm.num,hotel:ref_hotel.num}.to_json.to_s
       render text:ref
  end


  def welcome
      if session[:login] == "" or session[:login] == nil
          reset_session
          redirect_to login_path
          return
      end
      @current_url = "/"
  end

  def logout
      cookies.delete(:auth_token)
      session[:login] = nil
      session[:name] = nil
      flash.notice = "退出成功！"
      redirect_to login_path
  end

  def index
       @current_url = "/page"
  end
  
end
